package cn.edu.zjx2.controller;

import cn.edu.mp.entity.Class;
import cn.edu.mp.entity.Grade;
import cn.edu.mp.entity.Major;
import cn.edu.mp.entity.Student;
import cn.edu.mp.service.ClassService;
import cn.edu.mp.service.GradeService;
import cn.edu.mp.service.MajorService;
import cn.edu.mp.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {
    
    @Autowired
    private StudentService studentService;
    @Autowired
    private ClassService classService;
    @Autowired
    private GradeService gradeService;
    @Autowired
    private MajorService majorService;
    
    @GetMapping("/page")
    public String page(Model model) {
        List<Student> students = studentService.list();
        List<Class> classes = classService.list();
        List<Grade> grades = gradeService.list();
        List<Major> majors = majorService.list();
        
        model.addAttribute("students", students);
        model.addAttribute("classes", classes);
        model.addAttribute("grades", grades);
        model.addAttribute("majors", majors);
        return "student/list";
    }
    
    @PostMapping
    @ResponseBody
    public boolean save(@RequestBody Student student) {
        return studentService.save(student);
    }
    
    @PutMapping
    @ResponseBody
    public boolean update(@RequestBody Student student) {
        return studentService.updateById(student);
    }
    
    @DeleteMapping("/{id}")
    @ResponseBody
    public boolean delete(@PathVariable Long id) {
        return studentService.removeById(id);
    }
    
    @GetMapping("/{id}")
    @ResponseBody
    public Student getById(@PathVariable Long id) {
        return studentService.getById(id);
    }
} 